package com.kzd.gtboon.main

import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.*
import com.google.android.material.tabs.TabLayout
import com.kzd.gtboon.R
import com.kzd.gtboon.custom.setOnClickAntiShakeListener
import com.kzd.gtboon.databinding.FragmentMainHomeBinding
import com.kzd.gtboon.jetpack.vm.HomeViewModel
import com.kzd.gtboon.main.home.HomeFeaturedFragment
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class MainHomeFragment : OwnerViewBindingFragment<FragmentMainHomeBinding>() {

    companion object {
        fun newInstance(): MainHomeFragment {
            return MainHomeFragment()
        }
    }

    val mViewModel: HomeViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentMainHomeBinding
        get() = FragmentMainHomeBinding::inflate

    override fun initView() {
        BarUtils.addMarginTopEqualStatusBarHeight(mViewBinding.flMainHomeBar)
    }

    override fun initData() {

        mViewBinding.tabMainHomeKind.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) = Unit

            override fun onTabUnselected(tab: TabLayout.Tab?) {
                tab?.customView = null
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val text: TextView = TextView(context).apply {
                    setTextColor(ColorUtils.getColor(R.color.white))
                    textSize = 18f
                    gravity = Gravity.CENTER
                    paint.isFakeBoldText = true
                }
                text.text = tab?.text
                tab?.customView = text
            }
        })

        mViewBinding.vpMainHomeBody.adapter = object : FragmentStatePagerAdapter(
            childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {

            override fun getItem(position: Int): Fragment =
                HomeFeaturedFragment.newInstance()

            override fun getCount(): Int = 1

            override fun getPageTitle(position: Int): CharSequence {
                return "精选"
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {}
        }

        mViewBinding.tabMainHomeKind.setupWithViewPager(mViewBinding.vpMainHomeBody)

        mViewBinding.ivMainHomeSearch.setOnClickAntiShakeListener {
            toHomeSearchActivity()
        }
    }


    override fun initViewMode() {
        mViewModel.changeBarStyleLiveData.observe(viewLifecycleOwner, { distance ->
            val alpha = if (distance > 255) 255 else distance
            val color = Color.argb(alpha, 41, 131, 255)
            mViewBinding.flMainHomeBar.setBackgroundColor(color)
            BarUtils.setStatusBarColor(requireActivity(), color)
        })
    }
}